//
//  YHAlertView.m
//  MoreCoin
//
//  Created by 林宁宁 on 2019/10/28.
//  Copyright © 2019 MoreCoin. All rights reserved.
//

#import "YHAlertView.h"
#import "UIButton+YH.h"
#import "YHDefines.h"
#import "YHConstant.h"
#import "YHConstant+Null.h"
#import "UIColor+YHStyle.h"
#import "UIFont+YH.h"

@interface YHAlertView()

@property (retain, nonatomic,readwrite) UILabel * contentText;//内容
@property (retain, nonatomic,readwrite) UILabel * contentHeadTitle;//顶部标题
@property (retain, nonatomic) UIScrollView * contentScrollV;//内容
@property (retain, nonatomic, readwrite) UIView * contentBtnsView;//底部按钮视图

/// 内容区域 大小
@property (assign, nonatomic) CGFloat contentShowWidth;

@property (retain, nonatomic, readwrite) UIColor * colorHeadTitle;
@property (retain, nonatomic, readwrite) UIColor * colorContentText;
@property (retain, nonatomic, readwrite) UIFont * fontHeadTitle;
@property (retain, nonatomic, readwrite) UIFont * fontContentText;

@property (weak, nonatomic) UIView * customView;

@end



@implementation YHAlertView



-(void)yh_setupUI{
    
    self.tapBgToClose = NO;
    self.tapBtnToClose = YES;

    
    self.titleToTopSpace = Adapted(20);
    self.titleToContentSpace = Adapted(8);
    self.contentToButtonSpace = Adapted(8);
    
    self.btnVerticalSpace = 0;
    self.showHeightMax = 300;
    self.showHeightMin = 40;

    self.contentWidth = 300;
    self.contentShowWidth = self.contentWidth - Adapted(25*2);
    self.btnHeight = Adapted(40);
    self.btnBottomSpace = Adapted(10);
    self.onlyOneBtnBottomSpace = 0;//Adapted(-10);

    self.headTitleAligment = NSTextAlignmentLeft;
    self.contentAligment = NSTextAlignmentLeft;
    
    self.showBtnsSeparationLine = YES;
    self.isBlurShow = NO;

    self.colorHeadTitle = [UIColor yh_h1];
    self.colorContentText = [UIColor yh_h2];
    self.fontHeadTitle = [UIFont yh_pfmOfSize:17];
    self.fontContentText = [UIFont yh_pfOfSize:15];
    
    self.backgroundView.backgroundColor = [[UIColor yh_background] colorWithAlphaComponent:0.4];

    
    self.contentView.backgroundColor = [UIColor yh_background];
    self.contentView.layer.cornerRadius = 12;
    self.contentView.layer.masksToBounds = NO;
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.mas_equalTo(self.contentWidth);
    }];
    
    
    self.contentHeadTitle = [UILabel new];
    self.contentHeadTitle.textAlignment = self.headTitleAligment;
    self.contentHeadTitle.font = self.fontHeadTitle;
    self.contentHeadTitle.numberOfLines = 0;
    self.contentHeadTitle.lineBreakMode = NSLineBreakByWordWrapping;
    self.contentHeadTitle.textColor = self.colorHeadTitle;
    [self.contentView addSubview:self.contentHeadTitle];
    [self.contentHeadTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.contentShowWidth);
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.contentView).offset(self.titleToTopSpace);
        make.height.mas_equalTo(0);
    }];
    
    
    self.contentScrollV = [UIScrollView new];
    self.contentScrollV.showsVerticalScrollIndicator = NO;
    [self.contentView addSubview:self.contentScrollV];
    
    
    self.contentBtnsView = [UIView new];
    [self.contentView addSubview:self.contentBtnsView];
    [self.contentBtnsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).offset(-10);
        make.height.mas_equalTo(0);
    }];
    
    [self.contentScrollV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(self.contentShowWidth);
        make.centerX.equalTo(self.contentView);
        make.top.equalTo(self.contentHeadTitle.mas_bottom).offset(self.titleToContentSpace);
        make.bottom.equalTo(self.contentBtnsView.mas_top).offset(-self.contentToButtonSpace);
    }];
    
    
    self.contentText = [UILabel new];
    self.contentText.textAlignment = self.contentAligment;
    self.contentText.textColor = self.colorContentText;
    self.contentText.backgroundColor = [UIColor clearColor];

    self.contentText.font = self.fontContentText;

    self.contentText.numberOfLines = 0;

    self.contentText.lineBreakMode = NSLineBreakByWordWrapping;
    [self.contentScrollV addSubview:self.contentText];
    [self.contentText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentScrollV);
        make.width.equalTo(self.contentScrollV);
        make.centerX.equalTo(self.contentView);
    }];
    
    self.contentScrollV.backgroundColor = [UIColor clearColor];
    self.contentText.backgroundColor = [UIColor clearColor];
    self.contentHeadTitle.backgroundColor = [UIColor clearColor];
    self.contentBtnsView.backgroundColor = [UIColor clearColor];
    
    [self buildOtherContentV];
    
//    self.contentText.backgroundColor = [UIColor yh_randomColor];
//    self.contentHeadTitle.backgroundColor = [UIColor yh_randomColor];
//    self.contentScrollV.backgroundColor = [UIColor yh_randomColor];
//    self.contentBtnsView.backgroundColor = [UIColor yh_randomColor];

    [self styleSingleHeadTitleShow];
}

-(void)setTitleToContentSpace:(CGFloat)titleToContentSpace{
    _titleToContentSpace = titleToContentSpace;
    [self.contentScrollV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentHeadTitle.mas_bottom).offset(self.titleToContentSpace);
    }];
}

-(void)setContentToButtonSpace:(NSInteger)contentToButtonSpace{
    _contentToButtonSpace = contentToButtonSpace;
    
    [self.contentScrollV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentBtnsView.mas_top).offset(-contentToButtonSpace);
    }];
}

- (void)buildOtherContentV{
    
}

-(void)setContentWidth:(CGFloat)contentWidth{
    _contentWidth = contentWidth;
    [self.contentView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(contentWidth);
    }];
}

-(void)setContentAligment:(NSTextAlignment)contentAligment{
    _contentAligment = contentAligment;
    
    self.contentText.textAlignment = contentAligment;
}

-(void)setHeadTitleAligment:(NSTextAlignment)headTitleAligment{
    _headTitleAligment = headTitleAligment;
    
    self.contentHeadTitle.textAlignment = headTitleAligment;
}

/** 添加自定义视图显示弹窗*/
- (void)addCustomView:(UIView *)customV{
    
    self.customView = customV;
    
    self.contentText.attributedText = nil;
    self.contentText.text = nil;
    
    [self.contentText removeFromSuperview];

    
    [self.contentScrollV addSubview:customV];

    [customV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentScrollV);
        make.left.right.equalTo(self.contentView);
    }];
    
    CGSize contentSize = customV.frame.size;
    
    CGFloat heightText = contentSize.height;
    
    CGFloat contentHeight = MAX(heightText, self.showHeightMin);
    
    contentHeight = MIN(contentHeight, self.showHeightMax);

    
    [self.contentScrollV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(contentHeight);
        make.left.right.equalTo(self.contentView);
    }];
    
    self.contentScrollV.contentSize = CGSizeMake(contentSize.width, heightText);
    if(heightText <= contentHeight){
        [customV mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView);
            make.right.equalTo(self.contentView);
            make.height.mas_equalTo(heightText);
            make.centerY.equalTo(self.contentScrollV);
        }];
    }else{
        [customV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.contentScrollV);
            make.left.equalTo(self.contentView);
            make.right.equalTo(self.contentView);
        }];
    }
}

-(void)addHeadTitle:(NSString *)title{
    [self addHeadTitle:title attTitle:nil];
}
-(void)addHeadImageName:(NSString *)imageName{
    UIImage * image = [UIImage imageNamed:imageName];
    
    [self addHeadTitle:@"" attTitle:^(NSMutableAttributedString *attTitle) {
        [attTitle yh_paragraphBlock:^(NSMutableParagraphStyle *style) {
            style.alignment = NSTextAlignmentCenter;
        }];
        [attTitle yh_attachImage:image imageSize:CGSizeMake(image.size.width, image.size.height) atIndex:0 imageOffset:@(0)];
    }];
}

/**
 添加顶部标题
 */

- (void)addHeadTitle:(NSString *)title attTitle:(void (^_Nullable)(NSMutableAttributedString * ))attBlock{
    [self addHeadTitle:title titleWidth:self.contentShowWidth attTitle:attBlock];
}

- (void)addHeadTitle:(NSString *)title titleWidth:(CGFloat)width attTitle:(void (^)(NSMutableAttributedString *))attBlock{
    CGSize contentSize = CGSizeZero;
    if(attBlock){
        NSMutableAttributedString * attStr = [NSMutableAttributedString yh_initWithStr:title customBlock:^(NSMutableAttributedString *att) {
            [att yh_font:self.fontHeadTitle];
            [att yh_color:self.colorHeadTitle];
            if(attBlock){
                attBlock(att);
            }
        }];
        self.contentHeadTitle.attributedText = attStr;
        contentSize = [attStr yh_getSizeConstrainedToSize:CGSizeMake(width, FLT_MAX)];
    }else if (title){
        self.contentHeadTitle.text = title;

    }
    
    if(CGSizeEqualToSize(contentSize, CGSizeZero)){
        contentSize = [self.contentHeadTitle sizeThatFits:CGSizeMake(width, FLT_MAX)];
    }
    
    [self.contentHeadTitle mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(contentSize.height);
    }];
}



/**
 * 添加背景图，宽度可更改
 **/
- (void)addHeadBackgroundImage:(UIImage *)image{
    CGSize size = CGSizeMake(self.contentWidth, image.size.height * self.contentWidth / image.size.width);
    [self addHeadTitle:@"" titleWidth:self.contentWidth attTitle:^(NSMutableAttributedString *attTitle) {
        [attTitle yh_paragraphBlock:^(NSMutableParagraphStyle *style) {
            style.alignment = NSTextAlignmentCenter;
        }];
        [attTitle yh_attachImage:image imageSize:size atIndex:0 imageOffset:@(0)];
    }];
    
    [self.contentHeadTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(0);
        make.width.equalTo(@(self.contentWidth));
        make.centerX.equalTo(self.contentView);
    }];
}

/**
 添加内容
 */
- (void)addContent:(NSString *)content{
    [self addContent:content attContent:nil];
}

- (void)addContent:(NSString *)content attContent:(void (^)(NSMutableAttributedString *))attBlock{
    
    CGSize contentSize = CGSizeZero;
    if(attBlock){
        NSMutableAttributedString * attStr = [NSMutableAttributedString yh_initWithStr:content customBlock:^(NSMutableAttributedString *att) {
            [att yh_color:self.colorContentText];
            [att yh_font:self.fontContentText];
            if(attBlock){
                attBlock(att);
            }
        }];
        self.contentText.attributedText = attStr;
        contentSize = [attStr yh_getSizeConstrainedToSize:CGSizeMake(self.contentShowWidth, FLT_MAX)];
        contentSize.height = contentSize.height;

    }else if (content){
        self.contentText.text = content;

    }
    
    if(CGSizeEqualToSize(contentSize, CGSizeZero)){
        contentSize = [self.contentText sizeThatFits:CGSizeMake(self.contentShowWidth, FLT_MAX)];
    }
    
    CGFloat heightText = contentSize.height;

    
    CGFloat contentHeight = MAX(heightText, self.showHeightMin);
    contentHeight = MIN(contentHeight, self.showHeightMax);
    
    [self.contentScrollV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(contentHeight);
    }];
    
    self.contentScrollV.contentSize = CGSizeMake(contentSize.width, heightText);
    if(heightText <= contentHeight){
        [self.contentText mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.contentScrollV);
            if(self.contentVerticalLayoutCenter){
                make.centerY.equalTo(self.contentScrollV);
            }else{
                make.top.equalTo(self.contentScrollV);
            }
        }];
    }
}



-(void)addBtnTitle:(NSString *)btnTitle action:(void (^_Nullable)(YHAlertView *))action{
    [self addBtnBlock:^(UIButton *passBtn) {
        [passBtn setTitle:btnTitle forState:UIControlStateNormal];
        passBtn.titleLabel.font = [UIFont yh_pfOfSize:17];

    } action:action];
}
//- (void)addBtnGrayTitle:(NSString *)btnTitle action:(void (^)(YHAlertView *))action{
//    [self addBtnBlock:^(UIButton *passBtn) {
//        [passBtn setTitle:btnTitle forState:UIControlStateNormal];
//
//        passBtn.titleLabel.font = [UIFont yh_pfOfSize:17];
//        
//        [passBtn setTitleColor:[UIColor yh_8C8C8C] forState:UIControlStateNormal];
//    } action:action];
//}
- (void)addBtnBlueTitle:(NSString *)btnTitle action:(void (^)(YHAlertView *))action{
    [self addBtnBlock:^(UIButton *passBtn) {
        [passBtn setTitle:btnTitle forState:UIControlStateNormal];

        passBtn.titleLabel.font = [UIFont yh_pfmOfSize:17];
        [passBtn setTitleColor:[UIColor yh_blue] forState:UIControlStateNormal];
    } action:action];
}
- (void)addBtnOrangeTitle:(NSString *)btnTitle action:(void (^)(YHAlertView *))action{
    [self addBtnBlock:^(UIButton *passBtn) {
        [passBtn setTitle:btnTitle forState:UIControlStateNormal];

        passBtn.titleLabel.font = [UIFont yh_pfmOfSize:17];
        [passBtn setTitleColor:[UIColor yh_orange] forState:UIControlStateNormal];
    } action:action];
}
- (void)addBtnRedTitle:(NSString *)btnTitle action:(void (^)(YHAlertView *))action{
    [self addBtnBlock:^(UIButton *passBtn) {
        [passBtn setTitle:btnTitle forState:UIControlStateNormal];

        passBtn.titleLabel.font = [UIFont yh_pfmOfSize:17];

        [passBtn setTitleColor:[UIColor yh_red] forState:UIControlStateNormal];
    } action:action];
}

- (void)addBtnTitle:(NSString *)btnTitle{
    [self addBtnTitle:btnTitle action:nil];
}
- (void)addBtnBlueTitle:(NSString *)btnTitle{
    [self addBtnBlueTitle:btnTitle action:nil];
}

-(void)addBtnBack{
    [self addBtnTitle:LS(@"返回")];
}
- (void)addBtnCancel{
    [self addBtnTitle:LS(@"取消")];
}

-(void)addBtnBlock:(void (^)(UIButton *))btnBlock action:(nonnull void (^)(YHAlertView * _Nonnull))action{
    
    UIButton * alertBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [alertBtn setTitleColor:[UIColor yh_h2] forState:UIControlStateNormal];
    alertBtn.titleLabel.font = [UIFont yh_pfmOfSize:15];
    
//    alertBtn.backgroundColor = [UIColor yh_randomColor];
    
    WS(weakSelf);
    [alertBtn addActionHandler:^(UIButton * sender, NSInteger tag) {
        if(action){
            action(weakSelf);
        }
        if(weakSelf.tapBtnToClose){
            [weakSelf popDismiss];
        }
    }];
    
    if(btnBlock){
        btnBlock(alertBtn);
    }
    
    [self.contentBtnsView addSubview:alertBtn];
}

- (void)addCloseBtn{
    [self addCloseBtn:@selector(popDismiss) closeImg:nil];
}

- (void)addCloseBtnWithImgName:(NSString *)imgName{
    [self addCloseBtn:@selector(popDismiss) closeImg:[UIImage imageNamed:imgName]];
}

-(void)yh_popShowAnimation{
    
    if(IsNull(self.contentHeadTitle.text)){
        //空的
        [self.contentHeadTitle mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView);
        }];
    }
    if(IsNull(self.contentText.text) && !self.customView){
        //空的
        [self.contentScrollV mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentHeadTitle.mas_bottom);
            make.bottom.equalTo(self.contentBtnsView.mas_top).offset(-10);
            make.height.mas_equalTo(0);
        }];
    }
    
    UIView *(^createLineV)(void) = ^(void){
        UIView * lineV = [UIView new];
        
        lineV.backgroundColor = [UIColor yh_gradientFromColor:[[UIColor yh_colorWithHexString:@"4D4D4D"] colorWithAlphaComponent:0.1] toColor:[[UIColor yh_colorWithHexString:@"4D4D4D"] colorWithAlphaComponent:0.3] withHeight:self.btnHeight+20];
         
        return lineV;
    };
    
    NSArray * subViews = self.contentBtnsView.subviews;
    
    if(subViews.count > 0 && self.showBtnsSeparationLine){
        UIView * lineV = createLineV();
        [self.contentBtnsView addSubview:lineV];

        [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.contentBtnsView);
            make.height.mas_equalTo(1);
        }];
    }
    
    if(self.isBtnVerticalLayout){
        if(subViews.count > 1){
            [subViews mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:self.btnHeight leadSpacing:self.btnVerticalSpace tailSpacing:self.btnVerticalSpace];
            [subViews mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.contentBtnsView).offset(self.btnVerticalEdgeSpace);
                make.right.equalTo(self.contentBtnsView).offset(-self.btnVerticalEdgeSpace);
            }];
        }else{
            [[subViews firstObject] mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.contentBtnsView);
                make.height.mas_equalTo(self.btnHeight);
                make.left.equalTo(self.contentBtnsView).offset(self.btnVerticalEdgeSpace);
                make.right.equalTo(self.contentBtnsView).offset(-self.btnVerticalEdgeSpace);
            }];
        }
        [self.contentBtnsView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.btnHeight*subViews.count+self.btnVerticalSpace*(subViews.count+1));
            make.bottom.equalTo(self.contentView).offset(-self.btnBottomSpace);
        }];
        
        if(self.showBtnsSeparationLine){
            for(NSInteger i = 1; i<subViews.count; i++){

                UIView * btnV = subViews[i];
                UIView * lineV = createLineV();
                [btnV addSubview:lineV];
                [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(btnV);
                    make.centerX.equalTo(btnV);
                    make.width.equalTo(btnV);//.multipliedBy(0.9);
                    make.height.mas_equalTo(1);
                }];
            }
        }
    }
    else if(subViews.count > 2 || self.isBtnVerticalLayout){
        //上下排序
        [subViews mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedItemLength:self.btnHeight leadSpacing:self.btnVerticalSpace tailSpacing:self.btnVerticalSpace];
        [subViews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentBtnsView).offset(self.btnVerticalEdgeSpace);
            make.right.equalTo(self.contentBtnsView).offset(-self.btnVerticalEdgeSpace);
        }];
        
        [self.contentBtnsView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.btnHeight*subViews.count+self.btnVerticalSpace*(subViews.count+1));
            make.bottom.equalTo(self.contentView).offset(-self.btnVerticalEdgeSpace);
        }];
        
        if(self.showBtnsSeparationLine){
            for(NSInteger i = 1; i<subViews.count; i++){
                UIView * btnV = subViews[i];
                UIView * lineV = createLineV();

                [btnV addSubview:lineV];
                [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(btnV);
                    make.centerX.equalTo(btnV);
                    make.width.equalTo(btnV);//.multipliedBy(0.9);
                    make.height.mas_equalTo(1);
                }];
            }
        }
    }else if (subViews.count > 1){
        //只有两个按钮 左右排序
        [subViews mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:5 leadSpacing:5 tailSpacing:5];
        [subViews mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.top.equalTo(self.contentBtnsView);
        }];
        [self.contentBtnsView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.btnHeight+10);
            make.bottom.equalTo(self.contentView);
        }];
        if(self.showBtnsSeparationLine){
            UIView * lineV = createLineV();
            [self.contentBtnsView addSubview:lineV];
            [lineV mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.equalTo(self.contentBtnsView);//.multipliedBy(0.7);
                make.center.equalTo(self.contentBtnsView);
                make.width.mas_equalTo(1);
            }];
        }
    }else if (subViews.count > 0){
        [[subViews firstObject] mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentBtnsView);
            make.bottom.equalTo(self.contentBtnsView).offset(self.onlyOneBtnBottomSpace);
            make.left.equalTo(self.contentBtnsView).offset(Adapted(14));
            make.right.equalTo(self.contentBtnsView).offset(Adapted(-14));
        }];
        [self.contentBtnsView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(self.btnHeight+self.btnBottomSpace);
            make.bottom.equalTo(self.contentView);
        }];
    }
    
    
    
    //更新标题顶部间距
    [self.contentHeadTitle mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(self.titleToTopSpace);
    }];
    
    //更新内容视图底部间距
    [self.contentScrollV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentBtnsView.mas_top).offset(-self.contentToButtonSpace);
    }];
    
    [super yh_popShowAnimation];
}


-(void)layoutSubviews{
    [super layoutSubviews];

    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        //        [self.contentScrollV scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

        [self.contentScrollV setContentOffset:CGPointZero animated:YES];
    });
}


/// 只有顶部大标题显示
- (void)styleSingleHeadTitleShow{
//    self.contentWidth = APP.info.s_width*0.8;
    self.headTitleAligment = NSTextAlignmentCenter;
    self.contentAligment = NSTextAlignmentCenter;
    self.titleToTopSpace = Adapted(25);
    self.titleToContentSpace = Adapted(20);
    self.contentToButtonSpace = Adapted(20);
    
}

+ (void)alertWithTitle:(NSString *)title message:(NSString *)message cancelTitle:(NSString *)cancelTitle actionTitles:(NSArray <NSString *> *)actionTitles attributedBlock:(void (^)(NSMutableAttributedString *att))attributedBlock handler:(void(^)(NSUInteger buttonIndex, NSString *buttonTitle))handler {
    
    YHAlertView * alertV = [YHAlertView new];
    if (IsNonNull(title)) {
        [alertV addHeadTitle:title];
    }
    if (IsNonNull(message)) {
        [alertV addContent:message attContent:attributedBlock];
    }
    if (IsNonNull(cancelTitle)) {
        [alertV addBtnBlock:^(UIButton *passBtn) {
            [passBtn setTitle:cancelTitle forState:UIControlStateNormal];
            passBtn.titleLabel.font = [UIFont yh_pfOfSize:17];

        } action:^(YHAlertView * _Nonnull alertV) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [alertV popDismiss];
                if (handler) {
                    handler(0, cancelTitle);
                }
            });
        }];
    }
    for (int i = 0; i < actionTitles.count; i++) {
        NSString *actionTitle = actionTitles[i];
        [alertV addBtnBlueTitle:actionTitle action:^(YHAlertView * _Nonnull alertV) {
            //TODO:去充值
            if (handler) {
                handler(i + IsNonNull(cancelTitle), actionTitle);
            }
        }];
    }
    [alertV popShow];
}


+ (void)showConfig:(void (^)(YHAlertView * _Nonnull))config{
    
    YHAlertView * alertView = [YHAlertView new];
    if(config){
        config(alertView);
    }
    [alertView popShow];
}

@end
